package shouxiestream;

import cn.hutool.core.collection.CollUtil;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class SimpleStream<T> {
    private List<T> dataList;

    private SimpleStream(List<T> list) {
        this.dataList = list;
    }

    public static <T> SimpleStream<T> of(List<T> list) {
        SimpleStream<T> dataList = new SimpleStream(list);
        return dataList;
    }

    public SimpleStream<T> filter(Predicate<T> predicate) {
        List<T> filterList = CollUtil.newArrayList();

        for (T t : this.dataList) {
            if (predicate.test(t)) {
                filterList.add(t);
            }
        }

        this.dataList = filterList;
        return this;
    }

    public <R> SimpleStream<R> map(Function<T, R> function) {
        List<R> mapList = CollUtil.newArrayList();
        for (T t : this.dataList) {
            mapList.add(function.apply(t));
        }

        SimpleStream<R> mapSimpleStream = new SimpleStream(mapList);
        return mapSimpleStream;
    }

    public void forEach(Consumer<T> consumer) {
        for (T t : this.dataList) {
            consumer.accept(t);
        }
    }

    public List<T> collect() {
        return this.dataList;
    }

    public static void main(String[] args) {
        SimpleStream.of(CollUtil.toList(1, 2, 3))
                .filter(i -> i >= 2)
                .map(i -> i * 2)
                .forEach(System.out::println);
    }
}
